package com.itheima.controller;



import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.service.MemberService;
import com.sun.org.apache.regexp.internal.RE;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * 会员管理
 */
@RequestMapping("/member")
@RestController
public class MemberController {

    @Reference
    private MemberService memberService;

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = memberService.findPage(queryPageBean);
        return pageResult;
    }

    /**
     * 添加
     * @param map
     * @return
     */
    @RequestMapping("/add")
    public Result add(@RequestBody Map map){
        String username = SecurityContextHolder.getContext().getAuthentication().getName();
        if (username == null || username.length() <= 0){
            return new Result(false,"请先登录");
        }
        // 将用户名添加到前台的档案号之前当作档案号
        map.put("fileNumber",username + (String)map.get("fileNumber"));
        memberService.backendAdd(map);
        return new Result(true, MessageConstant.ADD_MEMBER_SUCCESS);
    }

    /**
     * 根据id查询用户和对应的档案
     * @param id
     * @return
     */
    @RequestMapping("/findById")
    public Result findById(Integer id){
        Result result = memberService.findMemberAndRecordById(id);
        return result;
    }

    /**
     * 编辑档案
     * @param map
     * @return
     */
    @RequestMapping("/update")
    public Result update(@RequestBody Map map){
        Result result = memberService.update(map);
        return result;
    }

    /**
     * 删除档案
     * @param fileNumber
     * @return
     */
    @RequestMapping("/delete")
    public Result delete(String fileNumber){
        Result result = memberService.deleteFile(fileNumber);
        return result;
    }


}
